package com.nail.api.customMake.controller;

import com.alibaba.druid.util.StringUtils;
import com.nail.api.customMake.bean.Indent;
import com.nail.api.customMake.bean.PhoneModel;
import com.nail.api.customMake.bean.PhoneTexture;
import com.nail.api.customMake.service.IPhoneModelService;
import com.nail.api.customMake.service.IPhoneTextureService;
import com.nail.api.user.bean.User;
import com.nail.client.RedisClient;
import com.nail.common.Constant;
import com.nail.controller.BaseController;
import com.nail.utils.IPUtils;
import com.nail.utils.PageResult;
import com.nail.utils.Result4API;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by jie on 2017/9/17.
 */
@Controller
@RequestMapping("/api/phoneTexture")
public class PhoneTextureController extends BaseController{
    Logger logger = Logger.getLogger(this.getClass());


    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private IPhoneTextureService phoneTextureService;

    @Autowired
    private RedisClient redisClient;

    @PostMapping(value="/addPhoneTexture")
    @ResponseBody
    public Result4API addPhoneTexture(HttpServletRequest request, PhoneTexture phoneTexture, String token){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：addPhoneTexture" );

        if(phoneTexture == null || phoneTexture.getName() == null ){
            result.setCode(Constant.API_CODE_501);
            result.setMsg(Constant.API_MSG_501);
            return result;
        }
        User user = (User)redisClient.get(Constant.REDIS_USER_KEY + token);

        //无权访问
        if(user.getRole() == null || !user.getRole().getId().equals(1L)){
            result.setCode(Constant.API_CODE_1003);
            result.setMsg(Constant.API_MSG_1003);
            return result;
        }

        phoneTexture.setUserName(user.getUserName());
        phoneTexture.setUserId(user.getId());
        phoneTexture.setCreateTime(new Date());

        try {
            phoneTextureService.addPhoneTexture(phoneTexture);
            result.setCode(200);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }


    @PostMapping(value="/updatePhoneTexture")
    @ResponseBody
    public Result4API updatePhoneTexture(HttpServletRequest request, PhoneTexture phoneTexture, String token){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：updatePhoneTexture" );

        if(phoneTexture == null || phoneTexture.getName() == null || phoneTexture.getId() == null){
            result.setCode(Constant.API_CODE_501);
            result.setMsg(Constant.API_MSG_501);
            return result;
        }
        User user = (User)redisClient.get(Constant.REDIS_USER_KEY + token);

        //无权访问
        if(user.getRole() == null || !user.getRole().getId().equals(1L)){
            result.setCode(Constant.API_CODE_1003);
            result.setMsg(Constant.API_MSG_1003);
            return result;
        }

        phoneTexture.setModifier(user.getId());
        phoneTexture.setUpdateTime(new Date());

        try {
            phoneTextureService.updatePhoneTexture(phoneTexture);
            result.setCode(200);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }


    @PostMapping(value="/deletePhoneTexture")
    @ResponseBody
    public Result4API deletePhoneTexture(HttpServletRequest request, Long id, String token){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：deletePhoneTexture" );

        if(id == null || StringUtils.isEmpty(token)){
            result.setCode(Constant.API_CODE_501);
            result.setMsg(Constant.API_MSG_501);
            return result;
        }
        User user = (User)redisClient.get(Constant.REDIS_USER_KEY + token);

        //无权访问
        if(user.getRole() == null || !user.getRole().getId().equals(1L)){
            result.setCode(Constant.API_CODE_1003);
            result.setMsg(Constant.API_MSG_1003);
            return result;
        }

        try {
            phoneTextureService.deletePhoneTexture(id);
            result.setCode(200);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }


    @PostMapping(value="/getPhoneTexture")
    @ResponseBody
    public Result4API getPhoneTexture(HttpServletRequest request,String token,Integer pageSize,Integer pageNo){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：getPhoneTexture" );
        PageResult pageResult = new PageResult();
        if(pageNo != null){
            pageResult.setPageNo(pageNo);
        }
        if(pageSize != null){
            pageResult.setPageSize(pageSize);
        }
        try {
            List<PhoneTexture> lists = phoneTextureService.getPhoneTextures(pageResult);
            result.setCode(200);
            result.setData(lists);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }



    @PostMapping(value="/getPhoneTextureById")
    @ResponseBody
    public Result4API getPhoneTextureById(HttpServletRequest request,String token,Long id){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：getPhoneTextureById" );

        try {
            PhoneTexture phoneTexture = phoneTextureService.getPhoneTextureById(id);
            result.setCode(200);
            result.setData(phoneTexture);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }



    @PostMapping(value="/setSort")
    @ResponseBody
    public Result4API setSort(HttpServletRequest request,Long id,Integer sort,String token){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：setSort" );

        if(sort ==null || id == null){
            result.setCode(Constant.API_CODE_501);
            result.setMsg(Constant.API_MSG_501);
            return result;
        }

        User user = (User)redisClient.get(Constant.REDIS_USER_KEY + token);

        //无权访问
        if(user.getRole() == null || !user.getRole().getId().equals(1L)){
            result.setCode(Constant.API_CODE_1003);
            result.setMsg(Constant.API_MSG_1003);
            return result;
        }

        try {
            phoneTextureService.setSort(id,sort);
            result.setCode(200);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }


    @PostMapping(value="/setIsAble")
    @ResponseBody
    public Result4API setIsAble(HttpServletRequest request,Long id,Integer isAble,String token){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：setIsAble" );

        if(isAble ==null || id == null){
            result.setCode(Constant.API_CODE_501);
            result.setMsg(Constant.API_MSG_501);
            return result;
        }

        User user = (User)redisClient.get(Constant.REDIS_USER_KEY + token);

        //无权访问
        if(user.getRole() == null || !user.getRole().getId().equals(1L)){
            result.setCode(Constant.API_CODE_1003);
            result.setMsg(Constant.API_MSG_1003);
            return result;
        }

        try {
            phoneTextureService.setIsAble(id,isAble);
            result.setCode(200);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }



}
